package com.xuy.ridis.redisdemo.utils;

import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.xuy.ridis.redisdemo.config.JedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.xuy.ridis.redisdemo.utils
 * @类名称：RedisUtils
 * @创建人：YIHANG.XU
 * @创建时间：2021/10/172:26 下午
 * @修改人：YIHANG.XU
 * @修改时间：2021/10/172:26 下午
 * @修改备注：
 * @Copyright
 */
@Component
public class RedisPoolUtils {
    @Autowired
    private JedisConfig jedisConfig;

    private static JedisPool jedisPool;

    public Jedis getJedisPool() {
        if (jedisPool == null) {
            jedisPool = newJedisPool();
        }
        return jedisPool.getResource();
    }

    private JedisPool newJedisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(jedisConfig.getMaxLib());
        jedisPoolConfig.setMaxTotal(jedisConfig.getMaxTotal());
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, jedisConfig.getHost(), jedisConfig.getPort(), jedisConfig.getTimeOut());

        return jedisPool;

    }


}
